<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BlinkController extends Controller
{
    /**
     * 展示友情链接列表
     *
     * @return object
     */
    public function getIndex(request $request)
    {
        //获取提交数据
        $strip = $request->input('strip','10');
        $keywords = $request->input('keywords');

        if($keywords){
            //查询数据库
            $datas = DB::table('blink')->where('name','like','%'.$keywords.'%')->paginate($strip);
        }else{
            $datas = DB::table('blink')->paginate($strip);
        }
       
       return view('Admin.blink.index',['datas'=>$datas,'pages'=>$request->all()]);
    }
    /**
     * 展示友情链接添加
     *
     * @return 友情链接添加视图
     */
    public function getAdd()
    {
        return view('admin.blink.add');
    }

     /**
     * 执行数据录入入库
     *
     * @return 录入数据ID
     */
    public function postInsert(request $request)
    {
        //获取提交数据
        $data = $request->except('_token');

        //执行数据入库
        $num = DB::table('blink')->insertGetId($data);

        if($num){
            return redirect('/admin/blink/index')->with('success','链接添加成功');
        }else{
            return redirect('/admin/blink/add')->with('error','链接添加失败');
        }
    }

    /**
     * 执行删除操作
     *
     * @return 受影响行数
     */
    public function getDel($id)
    {
        //判断删除权限
        
        //删除数据
        $num = DB::table('blink')->where('id',$id)->delete();

        if($num){
            return redirect('/admin/blink/index')->with('success','链接删除成功');
        }else{
            return redirect('/admin/blink/index')->with('error','链接删除失败');
        }
    }
    
    /**
     * 展示友情链接修改
     *
     * @return object
     */
    public function getEdit($id)
    {
        //查询数据库
        $datas = DB::table('blink')->where('id',$id)->first();

        return view('Admin.blink.edit',['datas'=>$datas]);
    }

     /**
     * 展示友情链接修改
     *
     * @return object
     */
    public function postUpdate(request $request)
    {
        //获取需要数据
        $data = $request->except(['_token','id']);

        //执行删除
        $num = DB::table('blink')->where('id',$request->input('id'))->update($data);

        if($num){
            return redirect('/admin/blink/index')->with('success','链接修改成功');
        }else{
            return redirect('/admin/blink/index')->with('error','链接修改失败');
        }
    }
}
